//获取所有游戏
function getAllGames() {
    $.get(
        "http://localhost:8080/queryAllGames",
        function (result) {
            dispayGames(result)
        }
    )
}

function dispayGames(result) {
    initCard()
    for (var i = 0; i < result.length; i++) {
        gName = result[i].gameName
        price = result[i].gamePrice + " 元"
        intro = result[i].gameIntro
        gameId = result[i].gameId
        imgSrc = "img/" + gName + ".jpg"

        $("div#item div#info:last div div p").html(intro)
        $("div#item div#info:last div img").attr("src", imgSrc)
        $("div#item div#info:last div div div small").html(price)
        $("div#info:last div div div div span#gid").html(gameId)
        $("div#info:last div div div div span#gname").html(gName)

        addCard()
    }

    $("div#item div#info:last").remove()
}

//添加一个卡片
function addCard() {
    $("div#item div#info:last").after('<div class="col-md-4" id="info">' +
        '<div class="card mb-4 shadow-sm">' +
        '<img src="" width="100%">' +
        '<div class="card-body">' +
        '<p class="card-text">介绍</p>' +
        '<div class="d-flex justify-content-between align-items-center">' +
        '<div class="btn-group">' +
        '<span style="display:none" id="gid">gid</span>' +
        '<span style="display:none" id="gname">gname</span>' +
        '<button type="button" class="btn btn-sm btn-outline-success" id="pur">购买</button>' +
        '<button type="button" class="btn btn-sm btn-outline-secondary" id="coll">收藏</button>' +
        '</div>' +
        '<small class="text-muted"></small>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '</div>')
    collGame()
}

//一个卡片都没有的时候，生成一个卡片
function initCard() {
    $("div#item").append('<div class="col-md-4" id="info">' +
        '<div class="card mb-4 shadow-sm">' +
        '<img src="" width="100%">' +
        '<div class="card-body">' +
        '<p class="card-text">介绍</p>' +
        '<div class="d-flex justify-content-between align-items-center">' +
        '<div class="btn-group">' +
        '<span style="display:none" id="gid">gid</span>' +
        '<span style="display:none" id="gname">gname</span>' +
        '<button type="button" class="btn btn-sm btn-outline-success" id="pur">购买</button>' +
        '<button type="button" class="btn btn-sm btn-outline-secondary" id="coll">收藏</button>' +
        '</div>' +
        '<small class="text-muted"></small>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '</div>')
    collGame()
}

//搜索游戏
function searchGame() {
    event.preventDefault()//防止按钮的默认动作
    $("div#page div#item #info").remove()//删除所有卡片
    gameName = $("input#gSearch").val()

    $.get(
        "http://localhost:8080/searchGame",
        { "gameName": gameName },
        function (result) {
            if (!$.isEmptyObject(result)) {
                dispayGames(result)
            }
            else {
                $("div#item").append("<h3 id='info'> 没有找到任何符合项 </h3>")
            }
        }
    )
}

//收藏游戏
function collGame() {
    $("button#coll:last").on("click", function () {
        if ($.cookie('user_id')) {
            gameId = $(this).siblings("span#gid").html()
            userId = $.cookie('user_id')

            $.get(
                "http://localhost:8080/addCollect",
                { "userId": userId, "gameId": gameId },
                function (result) {
                    if (result.code == 0) {
                        alert(result.message)
                    } else {
                        alert(result.message)
                    }
                }
            )
        } else {
            alert("未登录")
        }

    })

    $("button#pur:last").on("click", function () {
        gameId = $(this).siblings("span#gid").html()
        userId = $.cookie('user_id')
        gameName = $(this).siblings("span#gname").html()
        price = $(this).parent().siblings("small").html()

        $.cookie('game_id', gameId)
        $.cookie('game_name', gameName)
        $.cookie('game_price', price)

        window.location.replace("checkout.html")
    })
}